﻿using Ldt.DataEngine.LogAssembly.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using NLog;
namespace ConfigInfoWebPlatform.Nlog
{
    public class LogHelper
    {
        /// <summary>
        /// 日志处理对象
        /// </summary>
        private ILog LogModel;
        /// <summary>
        /// 日志操作对象
        /// </summary>
        private static LogHelper LogHelperM { get; set; }

        /// <summary>
        /// 日志类型
        /// </summary>
        public enum logType
        {
            /// <summary>
            /// The error
            /// </summary>
            Error,
            /// <summary>
            /// The information
            /// </summary>
            Info,
            /// <summary>
            /// The debug
            /// </summary>
            Debug,
            /// <summary>
            /// The fatal
            /// </summary>
            Fatal,
            /// <summary>
            /// The trace
            /// </summary>
            Trace,
            /// <summary>
            /// The warn
            /// </summary>
            Warn
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="layoutRenderList">布局对象</param>
        public LogHelper(List<string> layoutRenderList,string connectionStr)
        {
            foreach (var layoutRender in layoutRenderList)
            {//循环添加布局对象
                LogLibFactory.AddOrUpdateLayoutRendererValue(layoutRender);
            }
            LogModel = LogLibFactory.Create();
            LogHelperM = this;
        }

        /// <summary>
        /// 进行更新NLog数据变量信息
        /// </summary>
        /// <param name="valuePairs">变量集合</param>
        public void UpdateLogVar(IDictionary<string, string> valuePairs)
        {
            LogLibFactory.UpdateVariable(valuePairs);
        }

        /// <summary>
        /// 写入日志操作;
        /// </summary>
        /// <param name="type">r日志类型</param>
        /// <param name="message">日志内容</param>
        /// <param name="DynamicAttribute">动态属性集合<属性名称，属性值></param>
        public void WriteLog(logType type, string message, params Tuple<string, string>[] DynamicAttribute)
        {
            var dic=DynamicAttribute.ToDictionary(a => a.Item1, b => b.Item2);
            int code = type.GetHashCode();
            var logty = (LogType)Enum.Parse(typeof(LogType), code.ToString());
            LogModel.WriteLogs(logty, dic, string.Format("{0} ", message));
        }
    }
}
